/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.<br>
 */
package com.ce.pms.system.renovation.web;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.exception.ConstraintViolationException;

import com.ce.common.util.BlankUtil;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.renovation.service.IApprovalItemConfigService;
import com.ce.pms.system.renovation.vo.ApprovalItemConfigVO;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述装修审核项目<br>
 * @author 易贤建 创建日期：2008-12-24<br>
 */
public class AuditingItemAction extends PmsBaseAction {
	public IApprovalItemConfigService approvalItemConfigService = null;
	/**
	 * 功能描述:初始化列表<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-29  <br>
	 */
	public String initList(@Read SearchVO searchVO,
						   @Read(key = "ec_crd",defaultValue="10")	int pageSize,
						   @Read(key = "ec_p", defaultValue="1")	int pageNo){
		searchVO.setPageSize(pageSize);
		searchVO.setPageNo(pageNo);
		Page page = approvalItemConfigService.queryApprovalItemList(searchVO);
		setAttribute("approvalItems", page.getResult());
		setAttribute("totalRows", Integer.valueOf(String.valueOf(page.getTotalCount())));
		return "list";
	}
	/**
	 * 功能描述:初始化添加页面<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-29  <br>
	 */
	public String initAdd(@Read ApprovalItemConfigVO approvalItemConfigVO){
		return "add";
	}

	public String initEdit(@Read(key="keyword")Long keyword){
		ApprovalItemConfigVO appVO = approvalItemConfigService.getApprovalItemConfigById(keyword);
		setAttribute("approvalItemVO", appVO);
		return "edit";
	}
	/**
	 * 功能描述:添加装修管理业务配置<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-29  <br>
	 */
	public String add(@Read ApprovalItemConfigVO approvalItemConfigVO){
		approvalItemConfigVO.setEmployeeIds(processEmployeIds());
		approvalItemConfigVO.setCreator(super.getUserId());
		approvalItemConfigService.add(approvalItemConfigVO);
		processOption(); //处理按钮类型
		return "result";
	}
	/**
	 * 功能描述:修改装修管理业务配置<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-29  <br>
	 */
	public String edit(@Read ApprovalItemConfigVO approvalItemConfigVO){
		approvalItemConfigVO.setEmployeeIds(processEmployeIds());
		approvalItemConfigVO.setModifier(super.getUserId());
		approvalItemConfigService.edit(approvalItemConfigVO);

		processOption(); //处理按钮类型
		return "result";
	}
	/**
	 * 功能描述:根据ID删除对象。<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-30  <br>
	 */
	public void delete(@Read(key="keyword")Long keyword){
		try{
			approvalItemConfigService.delete(keyword);
		}catch(BusinessException exception){
			throw exception;
		}catch(Exception exception){
			throw new BusinessException("system:approval_delete_is_reference");
		}
	}
	/**
	 * 功能描述: 验证装修项目名称不能一样<br>
	 * 有ID时,表示添加
	 * @param approvalItemConfigVO
	 * Create author:yixianjian <br>
	 * Create on:2009-1-4  <br>
	 */
	public void validateItemName(@Read ApprovalItemConfigVO approvalItemConfigVO){
		try{
			approvalItemConfigService.validationApprovalItemConfigItemName(approvalItemConfigVO);
			super.renderText("true");
		}catch(BusinessException exception){
			String errorMessage = exception.getMessage();
			super.renderText(super.getLocaleMessage(errorMessage));
		}
	}
	/**
	 * 功能描述:页面传过来的员工ID是一个用逗号分割的字符串,这里处理下。<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-30  <br>
	 */
	private List<Long> processEmployeIds(){
		String employeIdStr = getRequest().getParameter("employeIds");
		if(BlankUtil.isBlank(employeIdStr)) return null;

		List<Long> employeIds = new ArrayList<Long>();
		String[] ids = employeIdStr.split(",");
		for(String id : ids){
			employeIds.add(Long.valueOf(id));
		}
		return employeIds;
	}
	/**
	 * 功能描述: 处理页面传过来的按钮类型,页面有保存并新增、保存按钮,页面有按钮类型隐藏表单域<br>
	 * Create author:yixianjian <br>
	 * Create on:2008-12-30  <br>
	 */
	private void processOption(){
		String option = getRequest().getParameter("option");
		setAttribute("option", option);
	}
	public void setApprovalItemConfigService(
			IApprovalItemConfigService approvalItemConfigService) {
		this.approvalItemConfigService = approvalItemConfigService;
	}
}
